
/*************************************************/
/* Run regressions for On-the-Run Treasuries     */
/*************************************************/

%macro RegVol (indata);

data whole1;
set &indata;
StdRet90=StdRet-mStdRet90;     volume90=volume-mVolume90;    number90=number-mnumber90;    AV90=AV-mAV90;
StdRet180=StdRet-mStdRet180;   volume180=volume-mVolume180;  number180=number-mnumber180;  AV180=AV-mAV180;
StdRet360=StdRet-mStdRet360;   volume360=volume-mVolume360;  number360=number-mnumber360;  AV360=AV-mAV360;

proc sort data=whole1; by name date;

data whole2;
set whole1;
StdRet90L1=lag(StdRet90);    if name^=lag(name)   then StdRet90L1=.;      StdRet90L2=lag2(StdRet90);    if name^=lag2(name)  then StdRet90L2=.;
StdRet90L3=lag3(StdRet90);   if name^=lag3(name)  then StdRet90L3=.;      StdRet90L4=lag4(StdRet90);    if name^=lag4(name)  then StdRet90L4=.;
StdRet90L5=lag5(StdRet90);   if name^=lag5(name)  then StdRet90L5=.;      StdRet90L6=lag6(StdRet90);    if name^=lag6(name)  then StdRet90L6=.;
StdRet90L7=lag7(StdRet90);   if name^=lag7(name)  then StdRet90L7=.;      StdRet90L8=lag8(StdRet90);    if name^=lag8(name)  then StdRet90L8=.;
StdRet90L9=lag9(StdRet90);   if name^=lag9(name)  then StdRet90L9=.;      StdRet90L10=lag10(StdRet90);  if name^=lag10(name) then StdRet90L10=.;
StdRet90L11=lag11(StdRet90); if name^=lag11(name) then StdRet90L11=.;     StdRet90L12=lag12(StdRet90);  if name^=lag12(name) then StdRet90L12=.;

StdRet180L1=lag(StdRet180);    if name^=lag(name)   then StdRet180L1=.;      StdRet180L2=lag2(StdRet180);    if name^=lag2(name)  then StdRet180L2=.;
StdRet180L3=lag3(StdRet180);   if name^=lag3(name)  then StdRet180L3=.;      StdRet180L4=lag4(StdRet180);    if name^=lag4(name)  then StdRet180L4=.;
StdRet180L5=lag5(StdRet180);   if name^=lag5(name)  then StdRet180L5=.;      StdRet180L6=lag6(StdRet180);    if name^=lag6(name)  then StdRet180L6=.;
StdRet180L7=lag7(StdRet180);   if name^=lag7(name)  then StdRet180L7=.;      StdRet180L8=lag8(StdRet180);    if name^=lag8(name)  then StdRet180L8=.;
StdRet180L9=lag9(StdRet180);   if name^=lag9(name)  then StdRet180L9=.;      StdRet180L10=lag10(StdRet180);  if name^=lag10(name) then StdRet180L10=.;
StdRet180L11=lag11(StdRet180); if name^=lag11(name) then StdRet180L11=.;     StdRet180L12=lag12(StdRet180);  if name^=lag12(name) then StdRet180L12=.;

StdRet360L1=lag(StdRet360);    if name^=lag(name)   then StdRet360L1=.;      StdRet360L2=lag2(StdRet360);    if name^=lag2(name)  then StdRet360L2=.;
StdRet360L3=lag3(StdRet360);   if name^=lag3(name)  then StdRet360L3=.;      StdRet360L4=lag4(StdRet360);    if name^=lag4(name)  then StdRet360L4=.;
StdRet360L5=lag5(StdRet360);   if name^=lag5(name)  then StdRet360L5=.;      StdRet360L6=lag6(StdRet360);    if name^=lag6(name)  then StdRet360L6=.;
StdRet360L7=lag7(StdRet360);   if name^=lag7(name)  then StdRet360L7=.;      StdRet360L8=lag8(StdRet360);    if name^=lag8(name)  then StdRet360L8=.;
StdRet360L9=lag9(StdRet360);   if name^=lag9(name)  then StdRet360L9=.;      StdRet360L10=lag10(StdRet360);  if name^=lag10(name) then StdRet360L10=.;
StdRet360L11=lag11(StdRet360); if name^=lag11(name) then StdRet360L11=.;     StdRet360L12=lag12(StdRet360);  if name^=lag12(name) then StdRet360L12=.;

/***Set the dummy***/

proc sort data=whole2; by name; run;
proc univariate data=whole2 noprint;
by name;
var volume90 volume180 volume360;
output out=cutpoint1 pctlpts=50 pctlpre=R1-R3 pctlname=P50;

data cutpoint2 (keep=name R1P50 R2P50 R3P50);
set cutpoint1;

data whole3;
merge whole2 cutpoint2;
by name;
if volume90<=R1P50  then rankgap90=1;     if volume90>R1P50  then rankgap90=0;
if volume180<=R2P50 then rankgap180=1;    if volume180>R2P50 then rankgap180=0;
if volume360<=R3P50 then rankgap360=1;    if volume360>R3P50 then rankgap360=0;

Dvolume90=rankgap90*volume90;      Dnumber90=rankgap90*number90;     DAV90=rankgap90*AV90;
Dvolume180=rankgap180*volume180;   Dnumber180=rankgap180*number180;  DAV180=rankgap180*AV180;
Dvolume360=rankgap360*volume360;   Dnumber360=rankgap360*number360;  DAV360=rankgap360*AV360;


/***Do the regression to take care of the autocorrelation***/

/****For 90-day moving average****/
/**For Volume and Lags**/
proc model data=whole2;
StdRet90=c+a*volume90+d1*StdRet90L1+d2*StdRet90L2+d3*StdRet90L3+d4*StdRet90L4+d5*StdRet90L5+d6*StdRet90L6+d7*StdRet90L7+d8*StdRet90L8+d9*StdRet90L9+d10*StdRet90L10+d11*StdRet90L11+d12*StdRet90L12;
fit StdRet90/gmm kernel=(bart,26,0);
instruments volume90 StdRet90L1-StdRet90L12;
by name;
ods output parameterestimates=NWVolumeLag25;
quit;


/**For NT and Lags**/
proc model data=whole2;
StdRet90=c+a*number90+d1*StdRet90L1+d2*StdRet90L2+d3*StdRet90L3+d4*StdRet90L4+d5*StdRet90L5+d6*StdRet90L6+d7*StdRet90L7+d8*StdRet90L8+d9*StdRet90L9+d10*StdRet90L10+d11*StdRet90L11+d12*StdRet90L12;
fit StdRet90/gmm kernel=(bart,26,0);
instruments number90 StdRet90L1-StdRet90L12;
by name;
ods output parameterestimates=NWnumberLag25;
quit;


/**For AV and Lags**/
proc model data=whole2;
StdRet90=c+a*AV90+d1*StdRet90L1+d2*StdRet90L2+d3*StdRet90L3+d4*StdRet90L4+d5*StdRet90L5+d6*StdRet90L6+d7*StdRet90L7+d8*StdRet90L8+d9*StdRet90L9+d10*StdRet90L10+d11*StdRet90L11+d12*StdRet90L12;
fit StdRet90/gmm kernel=(bart,26,0);
instruments AV90 StdRet90L1-StdRet90L12;
by name;
ods output parameterestimates=NWAVLag25;
quit;


/**For NT-AV and Lags**/
proc model data=whole2;
StdRet90=c+a*number90+b*AV90+d1*StdRet90L1+d2*StdRet90L2+d3*StdRet90L3+d4*StdRet90L4+d5*StdRet90L5+d6*StdRet90L6+d7*StdRet90L7+d8*StdRet90L8+d9*StdRet90L9+d10*StdRet90L10+d11*StdRet90L11+d12*StdRet90L12;
fit StdRet90/gmm kernel=(bart,26,0);
instruments number90 AV90 StdRet90L1-StdRet90L12;
by name;
ods output parameterestimates=NWNTAVLag25;
quit;


%mend RegVol;


/****Main Program***/

%RegVol (RegData);
run;


